//
//  TribeViewController.m
//  yigou
//
//  Created by wangdianlei on 2017/1/4.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "TribeLeftViewController.h"
#import "CommendTribeCell.h"
#import "HotTribeCell.h"
#import "RQQZCell.h"
#import "TribeModel.h"
#import "ContentListCell.h"
#import "TitleCell.h"
#import "TitleView.h"
#import "ContentListModel.h"
#import "SearchViewController.h"
#import "CreateTribeViewController.h"
#import "TribeDetailViewController.h"
#import "MsgCenterViewController.h"
#import "MyAddTribeListViewController.h"
#import "ArticleWebViewController.h"
#import "TopicleWebViewController.h"
#import "IndexModel.h"
#define CommendTribe (@"CommendTribeCell")
#define MyCreateTribeList (@"MyCreateTribeListCell")
#define MyJointribeList (@"MyJointribeListCell")
#define RQQZ (@"RQQZCell")
#define ContentList (@"ContentListCell")
#define Title (@"TitleCell")
#define sectionHeaderHeight (15)
#define Arc4randomColor [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

@interface  TribeLeftViewController()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,CommendTribeDelegate,HotTribeDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (strong, nonatomic) TitleView *titleView;
@property (nonatomic,strong)NSMutableArray *hotData;
@property (nonatomic,strong)NSMutableDictionary *dataSource;
@property (assign, nonatomic) NSInteger page;
@property (assign, nonatomic) BOOL pageToEnd;


@end

@implementation TribeLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageToEnd = NO;
    self.page = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadNetworkDataCommendTribe];
    [self createUI];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(GuanZhuRQQZAction) name:@"GuanZhuRQQZ" object:nil];
}
- (void)GuanZhuRQQZAction{
    [self loadNetworkData];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

}

- (void)createUI{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height-64-49-50) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.tableView];
    [self addRefreshHeader:self.tableView];
    
}

#pragma -mark TableView DataSource & Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     if (indexPath.section == 0){
        return 180;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            return 60;
        }
        return 120;
    }else if (indexPath.section == 2){
        
        if (indexPath.row == 0) {
            return 60;
        }
        return 145;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TribeModel *model = [[TribeModel alloc]initWithDictionary:self.dataSource error:nil];
    if (indexPath.section == 0) {
        HotTribeCell *cell = GET_CELL(HotTribeCell, @"HotTribeCell");
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell fillDataWithArray:_hotData];
        cell.delegate = self;
        return cell;
        
    }else if (indexPath.section == 1){
       
        if (indexPath.row == 0) {
            TitleCell *cell = GET_CELL(TitleCell, Title);
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.titleLabel.text = @"人气酋长";
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);
            return cell;
        }else{
            RQQZCell *cell = GET_CELL(RQQZCell, RQQZ);
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            
            NSDictionary *dic = [model.rqqz objectAtIndex:indexPath.row-1];
            cell.model = [[RQQZModel alloc]initWithDictionary:dic error:nil];
            return cell;
        }
       
    }else if (indexPath.section == 2){
        
        if (indexPath.row == 0) {
            TitleCell *cell = GET_CELL(TitleCell, Title);
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.titleLabel.text = @"精选部落";
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);
            return cell;
        }else{
            CommendTribeCell *cell = GET_CELL(CommendTribeCell, CommendTribe);
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            NSLog(@"index:%d",indexPath.row - 1);
            NSDictionary *dic = [model.commendTribe objectAtIndex:indexPath.row-1];
            cell.model = [[CommendTribeModel alloc]initWithDictionary:dic error:nil];
            cell.delegate = self;
            return cell;
        }
        
        
    }
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    TribeModel *model = [[TribeModel alloc]initWithDictionary:self.dataSource error:nil];
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return model.rqqz.count+1;
            break;
        case 2:
            return model.commendTribe.count+1;
            break;
        default:
            break;
    }
    return 0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TribeModel *model = [[TribeModel alloc]initWithDictionary:self.dataSource error:nil];
    if (indexPath.section ==1&&indexPath.row != 0) {
        
        NSDictionary *dic = [model.rqqz objectAtIndex:indexPath.row-1];
        NSString *userID = [dic objectForKey:@"user_id"];
        [[GoToManager sharedGoToManager] go:self type:ToHisCenter data:userID];
        
    }else if (indexPath.section ==2){
        
        if (indexPath.row == 0) {
            return;
        }
        NSDictionary *dic = [model.commendTribe objectAtIndex:indexPath.row-1];
        NSString *tribeID = [dic objectForKey:@"tribe_id"];
        TribeDetailViewController *detail = [[TribeDetailViewController alloc]initWithNibName:@"TribeDetailViewController" bundle:nil];
        detail.tribeID = tribeID;
        [self.navigationController pushViewController:detail animated:YES];
    }
}
- (void)loadNetworkDataCommendTribe {
   
    DYBaseRequest *request = [DYBaseRequest new];
   
    request.requestUrl = [NSString stringWithFormat:@"%@/sys/index/hotTribe?size=%@", APIYIGOU,@"5"];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        _hotData = [NSMutableArray arrayWithArray:[dict objectForKey:@"data"]];
        __weak __typeof(self) weakself= self;
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakself.tableView reloadData];
            [weakself.tableView.header endRefreshing];
        });
        [self loadNetworkData];
    } error:^(YTKBaseRequest *request) {
        [self.tableView.header endRefreshing];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        [self.tableView.header endRefreshing];
    }];
}
- (void)loadNetworkData {
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = [NSString stringWithFormat:@"%@/hytribe/tribeInit", APIYIGOU];
    self.page = 1;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        NSDictionary *data = [dict objectForKey:@"data"];
        [self.dataSource removeAllObjects];
        self.dataSource = [NSMutableDictionary dictionaryWithDictionary:data];
        __weak __typeof(self) weakself= self;
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakself.tableView reloadData];
            [weakself.tableView.header endRefreshing];
        });
    } error:^(YTKBaseRequest *request) {
        [self.tableView.header endRefreshing];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        [self.tableView.header endRefreshing];
    }];
}

#pragma mark 上拉、下拉
- (void)refresh {
    [super refresh];
    [self.dataSource removeAllObjects];
    [self loadNetworkData];
}

#pragma mark 热门部落
-(void)didClickHotTribeWithTribeID:(NSString *)tribeID;{
    
    TribeDetailViewController *detail = [[TribeDetailViewController alloc]initWithNibName:@"TribeDetailViewController" bundle:nil];
    [self.navigationController pushViewController:detail animated:YES];
    detail.tribeID = tribeID;
}

#pragma mark- 精选部落
-(void)didClickCommendTribeWithTbibeID:(NSString *)tribeID{
    TribeDetailViewController *detail = [[TribeDetailViewController alloc]initWithNibName:@"TribeDetailViewController" bundle:nil];
    [self.navigationController pushViewController:detail animated:YES];
    detail.tribeID = tribeID;
}


@end
